package com.lj.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lj.eduservice.entity.Course;
import com.lj.eduservice.entity.CourseDescription;
import com.lj.eduservice.entity.vo.CourseInfoVo;
import com.lj.eduservice.entity.vo.CourseQuery;
import com.lj.eduservice.service.CourseDescriptionService;
import com.lj.eduservice.service.CourseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author luojing
 * @since 2021-05-06
 */
@RestController
@RequestMapping("/eduservice/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    /**
     * @param courseInfoVo
     * @return 添加课程之后需要添加课程大纲和小节，这里需要Course的id所以返回courseInfoVo
     */
    @ApiOperation("添加课程基本信息")
    @PostMapping("/addCourseInfo")
    public CourseInfoVo addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        courseService.saveOrUpdateCourseInfo(courseInfoVo);
        return courseInfoVo;   //携带了id
    }

    @ApiOperation("通过课程id查询课程信息")
    @GetMapping("/getCourseInfo/{courseId}")
    public CourseInfoVo getCourseInfo(@PathVariable("courseId") String courseId) {
        return courseService.getCourseInfo(courseId);
    }

    @ApiOperation("发布课程")
    @PostMapping("/publishCourse/{id}")
    public Boolean publishCourse(@PathVariable("id") String courseId) {
        return courseService.update(new Course(), new UpdateWrapper<Course>()
                .set("status", "Normal")
                .eq("id", courseId));
    }

    @ApiOperation("修改课程状态")
    @PostMapping("/updateCourseStatus/{id}")
    public Boolean updateCourseStatus(@PathVariable("id") String courseId, String status) {
        return courseService.update(new Course(), new UpdateWrapper<Course>()
                .set("status", status)
                .eq("id", courseId));
    }

    @GetMapping("/pageConditionCourse/{current}/{size}")
    @ApiOperation("分页条件获取课程信息")
    public Page<Course> pageConditionCourse(@PathVariable Long current, @PathVariable Long size, CourseQuery courseQuery) {
        Page<Course> coursePage = new Page<>(current, size);
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        //根据条件拼接 wrapper
        String title = courseQuery.getTitle();
        String teacherId = courseQuery.getTeacherId();
        String subjectId = courseQuery.getSubjectId();
        String status = courseQuery.getStatus();
        String begin = courseQuery.getBegin();
        String end = courseQuery.getEnd();
        if (!StringUtils.isEmpty(title)) {
            wrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(teacherId)) {
            wrapper.eq("teacher_id", teacherId);
        }
        if (!StringUtils.isEmpty(subjectId)) {
            wrapper.eq("subject_id", subjectId);
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }
        wrapper.orderByDesc("gmt_modified");
        courseService.page(coursePage, wrapper);
        return coursePage;
    }

    @ApiOperation("根据课程id删除课程")
    @DeleteMapping("/{courseId}")
    public Boolean deleteCourse(@PathVariable("courseId") String courseId){
        return courseService.removeCourseById(courseId);
    }

}

